package com.woniu.micropropertyserver.controller;

import com.github.pagehelper.PageInfo;

import com.woniu.microcommonsserver.entity.PropertyFaceCheck;
import com.woniu.micropropertyserver.bean.DployPface;
import com.woniu.micropropertyserver.feign.service.MicroUserServerFeign;
import com.woniu.micropropertyserver.service.IPropertyFaceCheckService;
import com.woniu.micropropertyserver.util.DateUtil;
import com.woniu.micropropertyserver.util.PageRest;
import com.woniu.micropropertyserver.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @author ：bb
 * @date ：Created in 2021/1/28 0029
 * @description：
 * @modified By：
 * @version: $
 */
@RequestMapping("/proface")
@RestController
public class PropertyFaceController {

    @Autowired
    private IPropertyFaceCheckService propertyFaceCheckServiceImpl;
    @Autowired
    private MicroUserServerFeign microUserServerFeign;

    @GetMapping(value = "/findByparams",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findByparams(PageRest pageRest , DployPface dployPface ){

        System.out.println(dployPface);
        PageInfo<DployPface> infos= propertyFaceCheckServiceImpl.findByParams(pageRest,dployPface);

        return new ResultMsg(200,"操作成功",infos);

    }

    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg save(@RequestBody PropertyFaceCheck propertyFaceCheck){
        System.out.println(propertyFaceCheck);
        int res = propertyFaceCheckServiceImpl.saveInfo(propertyFaceCheck);
        if(res>0){
            return new ResultMsg(200,"操作成功",null);
        }else{
            return new ResultMsg(200,"新增失败",null);
        }

    }


    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResultMsg updateDate(@RequestBody Map map) throws ParseException {
        System.out.println(map);
        Integer bulid = Integer.parseInt((String)map.get("bulid"));
        int res=0;
        Long[] ids = new Long[((ArrayList) map.get("ids")).size()];
        Date authorizeStartDate = null;
        Date authorizeEndDate = null;
        if (map != null) {
            authorizeStartDate = DateUtil.strToDate((String) map.get("authorizeStartDate"), "yyyy-MM-dd");
            authorizeEndDate = DateUtil.strToDate((String) map.get("authorizeEndDate"), "yyyy-MM-dd");

            System.out.println(map.get("ids"));
            if (map.get("bulid") != null) {
                bulid = Integer.parseInt((String) map.get("bulid"));
            }

            ArrayList strs = (ArrayList) map.get("ids");
            System.out.println("尺寸：" + strs.size());
            for (int i = 0; i < strs.size(); i++) {
                ids[i] = Long.parseLong(strs.get(i).toString());
            }
        }
            System.out.println("日期");
            System.out.println(authorizeStartDate);
            System.out.println(ids);

         res = propertyFaceCheckServiceImpl.updateAuth(ids,authorizeStartDate, authorizeEndDate,bulid);
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"更新失败",null);
        }

    }
    @PutMapping(value = "/cancel",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg cancelInfo(Long[] ids){
        System.out.println(ids);
        int res = propertyFaceCheckServiceImpl.delInfo(ids);
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"删除失败",null);
        }
    }
    @GetMapping(value = "/id",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg One(Long id){
        System.out.println(id);
        PropertyFaceCheck propertyFaceCheck = propertyFaceCheckServiceImpl.findOne(id);
        return   new ResultMsg(200,"删除失败",propertyFaceCheck);
    }
}
